package com.atguigu.tingshu.album.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.alibaba.fastjson.JSONObject;
import com.atguigu.tingshu.album.mapper.*;
import com.atguigu.tingshu.album.service.BaseCategoryService;
import com.atguigu.tingshu.common.cache.GuiGuCache;
import com.atguigu.tingshu.model.album.*;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import java.util.function.Consumer;
import java.util.stream.Collectors;

@Service
@SuppressWarnings({"all"})
public class BaseCategoryServiceImpl extends ServiceImpl<BaseCategory1Mapper, BaseCategory1> implements BaseCategoryService {

	@Autowired
	private BaseCategory1Mapper baseCategory1Mapper;

	@Autowired
	private BaseCategory2Mapper baseCategory2Mapper;

	@Autowired
	private BaseCategory3Mapper baseCategory3Mapper;

	@Autowired
	private BaseCategoryViewMapper baseCategoryViewMapper;

	@Autowired
	private BaseAttributeMapper baseAttributeMapper;


	/**
	 * 查询所有的1,2,3级分类列表
	 * @return
	 */
	@Override
	public List<JSONObject> getBaseCategoryList() {

		List<BaseCategoryView> baseCategoryViewList = baseCategoryViewMapper.selectList(null);

		List<JSONObject> jsonObjectList = new ArrayList<>();

		//首先根据查出来的大表进行过滤分组,分成以Category1Id为key的集合,集合中存的就是以Category1Id分组后的数据
		Map<Long, List<BaseCategoryView>> category1Map = baseCategoryViewList
				.stream()
				.collect(Collectors.groupingBy(BaseCategoryView::getCategory1Id));

		for (Map.Entry<Long, List<BaseCategoryView>> entry1 : category1Map.entrySet()) {
			//新建一个JSONObject对象用来存储遍历出来的Category1的信息
			JSONObject jsonObject1 = new JSONObject();
			//由上面分析可以知道键值对对象的key就是Category1的id,而Category1的name就在分组后的集合的任意一条的信息里都有
			//开始封装jsonObject1对象
			jsonObject1.put("categoryId", entry1.getKey());
			jsonObject1.put("categoryName", entry1.getValue().get(0).getCategory1Name());
			//自此jsonObject1对象的基础字段已经封装好了,但是还缺一个categoryChild字段没封装,这个字段封装的就是开始封装jsonObject2对象集合
			List<JSONObject> jsonObjectList2 = new ArrayList<>();
			//遍历entry1得到集合,得到集合后再根据Category2Id分组
			List<BaseCategoryView> baseCategoryViewList2 = entry1.getValue();
			Map<Long, List<BaseCategoryView>> category2Map = baseCategoryViewList2
					.stream()
					.collect(Collectors.groupingBy(BaseCategoryView::getCategory2Id));

			for (Map.Entry<Long, List<BaseCategoryView>> entry2 : category2Map.entrySet()) {
				JSONObject jsonObject2 = new JSONObject();
				jsonObject2.put("categoryId", entry2.getKey());
				jsonObject2.put("categoryName", entry2.getValue().get(0).getCategory2Name());


				//重复上述步骤,封装jsonObject3
				List<JSONObject> jsonObjectList3 = new ArrayList<>();

				List<BaseCategoryView> baseCategoryViewList3 = entry2.getValue();

				Map<Long, List<BaseCategoryView>> category3Map = baseCategoryViewList3
						.stream()
						.collect(Collectors.groupingBy(BaseCategoryView::getCategory3Id));

				for (Map.Entry<Long, List<BaseCategoryView>> entry3 : category3Map.entrySet()) {
					JSONObject jsonObject3 = new JSONObject();

					jsonObject3.put("categoryId", entry3.getKey());
					jsonObject3.put("categoryName", entry3.getValue().get(0).getCategory3Name());
					jsonObjectList3.add(jsonObject3);
				}
				jsonObject2.put("categoryChild",jsonObjectList3);
				jsonObjectList2.add(jsonObject2);

			}
			jsonObject1.put("categoryChild",jsonObjectList2);
			jsonObjectList.add(jsonObject1);
		}

		return jsonObjectList;
	}


	/**
	 * 根据一级分类Id获取分类属性以及属性值
	 * @return
	 */
	@Override
	public List<BaseAttribute> findAttribute(Long category1Id) {

		List<BaseAttribute> baseAttributeList = baseAttributeMapper.findAttribute(category1Id);

		return baseAttributeList;
	}

	@Override
	@GuiGuCache(prefix = "album:categoryview:",ttl = 3600, timeunit = TimeUnit.SECONDS)
	public BaseCategoryView getCategoryView(Long category3Id) {
		BaseCategoryView baseCategoryView = baseCategoryViewMapper.selectById(category3Id);

		return baseCategoryView;
	}

	/**
	 * 根据一级分类Id查询置顶7个三级分类列表
	 *
	 * @param category1Id
	 * @return
	 */
	@Override
	public List<BaseCategory3> findTopBaseCategory3(Long category1Id) {

		//1.根据 category1Id 查询出二级分类 再查询出三级分类
		//最终要封装为BaseCategory3

		//2.先查询出与 category1Id 关联的 BaseCategory2 的 id集合
		List<Long> baseCategory2IdList = baseCategory2Mapper.selectList(new LambdaQueryWrapper<BaseCategory2>()
				.eq(BaseCategory2::getCategory1Id, category1Id)
		).stream().map(
				c2 -> {
					return c2.getId();
				}
		).collect(Collectors.toList());

		if (CollUtil.isNotEmpty(baseCategory2IdList)){
			//3.根据查出来的 baseCategory2Id 把 BaseCategory3 的对象查询出来 然后封装对象
			//注意 : 只要7个 并且根据orderNum排序

			LambdaQueryWrapper<BaseCategory3> wrapper = new LambdaQueryWrapper<>();
			wrapper.in(BaseCategory3::getCategory2Id,baseCategory2IdList);
			wrapper.eq(BaseCategory3::getIsTop , 1);
			wrapper.orderByDesc(BaseCategory3::getOrderNum);
			wrapper.last("limit 7");

			List<BaseCategory3> baseCategory3List = baseCategory3Mapper.selectList(wrapper);

			return baseCategory3List;
		}

		return null;
	}

	/**
	 * 查询1级分类下包含所有二级以及三级分类
	 * @param category1Id
	 * @return
	 */
	@Override
	public JSONObject getBaseCategoryListSearch(Long category1Id) {

		//1. 想要实现这个功能,最大的难点就是Child字段的封装,一层套一层,最好的办法就是用fastJson封装,又简单效率又高
		//先准备好一个jsonObject对象
		JSONObject jsonObject = new JSONObject();

		//根据category1Id获取到所有的category1集合
		//走的是视图不是一个一个的表
		List<BaseCategoryView> baseCategory1List = baseCategoryViewMapper.selectList(new LambdaQueryWrapper<BaseCategoryView>()
				.eq(BaseCategoryView::getCategory1Id, category1Id)
		);

		if (CollUtil.isNotEmpty(baseCategory1List)){
			//查出来的这个集合是category1Id 为传入的值的集合\
			jsonObject.put("categoryId", category1Id);
			jsonObject.put("categoryName", baseCategory1List.get(0).getCategory1Name());
			Map<Long, List<BaseCategoryView>> baseCategory2Map = baseCategory1List.stream().collect(Collectors.groupingBy(BaseCategoryView::getCategory2Id));

			List<JSONObject> jsonObjectList2 = new ArrayList<>();
			for (Map.Entry<Long, List<BaseCategoryView>> entry2 : baseCategory2Map.entrySet()) {
				JSONObject jsonObject2 = new JSONObject();
				jsonObject2.put("categoryId",entry2.getKey());
				jsonObject2.put("categoryName",entry2.getValue().get(0).getCategory2Name());

				List<BaseCategoryView> baseCategoryViewList2 = entry2.getValue();

				List<JSONObject> jsonObjectList3 = new ArrayList<>();
				for (BaseCategoryView baseCategoryView2 : baseCategoryViewList2) {
					JSONObject jsonObject3 = new JSONObject();
					jsonObject3.put("categoryId",baseCategoryView2.getCategory3Id());
					jsonObject3.put("categoryName",baseCategoryView2.getCategory3Name());

					jsonObjectList3.add(jsonObject3);
				}

				jsonObject2.put("categoryChild",jsonObjectList3);

				jsonObjectList2.add(jsonObject2);
			}

			jsonObject.put("categoryChild",jsonObjectList2);


			return jsonObject;
		}
		return null;
	}


}
